package com.xinghai.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.xinghai.common.exception.ServiceException;
import java.io.IOException;

import com.xinghai.common.utils.DateUtils;
import com.xinghai.common.utils.PageUtils;
import com.xinghai.domain.JkqActivity;
import com.xinghai.domain.JkqUser;
import com.xinghai.service.IJkqActivityService;
import com.xinghai.service.IJkqUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.domain.JkqActivityShare;
import com.xinghai.service.IJkqActivityShareService;
import com.xinghai.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;
import com.xinghai.common.core.page.TableDataInfo;

/**
 * 活动分享记录Controller
 *
 * @author xinghai
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/system/activityShare")
public class JkqActivityShareController extends BaseController
{
    @Autowired
    private IJkqActivityShareService jkqActivityShareService;

    @Autowired
    private IJkqActivityService jkqActivityService;

    @Autowired
    private IJkqUserService jkqUserService;

    /**
     * 查询活动分享记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:list')")
    @GetMapping("/list")
    public AjaxResult list(JkqActivityShare jkqActivityShare)
    {
        List<JkqActivityShare> list = jkqActivityShareService.list(jkqActivityShare);
        return success(list);
    }

    /**
     * 查询活动地址管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:activityAddress:list')")
    @GetMapping("/activityList")
    public AjaxResult activityList(JkqActivity jkqActivity)
    {
        PageUtils.orderBy("create_time desc");
        //jkqActivity.setStatus("0");
        List<JkqActivity> list = jkqActivityService.list(jkqActivity);
        return success(list);
    }

    /**
     * 查询徽章管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:badge:list')")
    @GetMapping("/userList")
    public AjaxResult userList(JkqUser jkqUser)
    {
        if(jkqUser.getType()==null){
            //最大五十条
            PageUtils.startPage(1,50);
        }
        List<JkqUser> list = jkqUserService.list(jkqUser);
        return success(list);
    }

    /**
     * 查询活动分享记录列表(带分页)
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:list')")
    @GetMapping("/page")
    public TableDataInfo page(JkqActivityShare jkqActivityShare)
    {
        startPage();
        List<JkqActivityShare> list = jkqActivityShareService.list(jkqActivityShare);
        return getDataTable(list);
    }

    /**
     * 导出活动分享记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:export')")
    @Log(title = "活动分享记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JkqActivityShare jkqActivityShare)
    {
        List<JkqActivityShare> list = jkqActivityShareService.list(jkqActivityShare);
        ExcelUtil<JkqActivityShare> util = new ExcelUtil<JkqActivityShare>(JkqActivityShare.class);
        util.exportExcel(response, list, "活动分享记录数据");
    }

    /**
     * 获取活动分享记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:query')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(jkqActivityShareService.getById(id));
    }

    /**
     * 新增活动分享记录
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:add')")
    @Log(title = "活动分享记录", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody JkqActivityShare jkqActivityShare)
    {
        jkqActivityShare.setCreateTime(DateUtils.getNowDate());
        jkqActivityShare.setCreateBy(getUsername());
        return toAjax(jkqActivityShareService.save(jkqActivityShare));
    }

    /**
     * 修改活动分享记录
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:edit')")
    @Log(title = "活动分享记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody JkqActivityShare jkqActivityShare)
    {
        jkqActivityShare.setUpdateTime(DateUtils.getNowDate());
        jkqActivityShare.setUpdateBy(getUsername());
        return toAjax(jkqActivityShareService.updateById(jkqActivityShare));
    }

    /**
     * 删除活动分享记录
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:remove')")
    @Log(title = "活动分享记录", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(jkqActivityShareService.removeById(id));
    }


    /**
     * 批量删除活动分享记录
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:remove')")
    @Log(title = "活动分享记录", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(jkqActivityShareService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 导入活动分享记录模板
     */
    @PreAuthorize("@ss.hasPermi('system:activityShare:import')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<JkqActivityShare> util = new ExcelUtil<JkqActivityShare>(JkqActivityShare.class);
        util.importTemplateExcel(response, "活动分享记录数据模板");
    }


    /**
     * 导入活动分享记录
     */
    @Log(title = "活动分享记录管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:activityShare:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<JkqActivityShare> util = new ExcelUtil<JkqActivityShare>(JkqActivityShare.class);
        List<JkqActivityShare> jkqActivityShareList = null;
        try {
            jkqActivityShareList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(jkqActivityShareService.importData(jkqActivityShareList));
    }
}
